package com.maila.calltaxi.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.maila.calltaxi.R;
import com.maila.calltaxi.utils.NullCheckUtil;
import com.maila.calltaxi.utils.RegularUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改手机号pop
 */

public class ChangePhonePop extends PopupWindow {


    private View mMenuView;
    int step = 0;
    String phone = "";
    String code = "";
    private int countdown = 60;
    Timer mTimer;
    boolean isSendSuccess = false;
    ViewHolder vh;
    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                isSendSuccess = false;
                vh.btnGetcode.setEnabled(true);
                vh.btnGetcode.setClickable(true);
                vh.btnGetcode.setBackgroundResource(R.color.text_three);
                vh.btnGetcode.setText("重新发送");
                if (mTimer != null) {
                    mTimer.cancel();
                    mTimer = null;
                }
            } else {
                vh.btnGetcode.setEnabled(false);
                vh.btnGetcode.setBackgroundResource(R.color.dark_line);
                vh.btnGetcode.setText(msg.what + "秒");
            }
        }
    };

    public ChangePhonePop(final Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.view_change_phone, null);
        AutoUtils.autoSize(mMenuView);
        vh = new ViewHolder(mMenuView);
        mMenuView.setTag(vh);
        vh.btnNext.setClickable(false);
        vh.btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        vh.btnGetcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isSendSuccess) {
                    countdown = 60;
                    mTimer = new Timer();
                    mTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            if (countdown >= 0) {
                                mHandler.sendEmptyMessage(countdown);
                                countdown--;
                            }
                        }
                    }, 0, 1000);
                }

            }
        });
        vh.btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (step == 0) {
                    vh.btnNext.setClickable(false);
                    vh.btnNext.setBackgroundResource(R.color.drive_line);
                    vh.btnGetcode.setVisibility(View.VISIBLE);
                    step = 1;
                    vh.etCode.setText("");
                    vh.tvTitle.setText("输入验证码");
                    vh.etCode.setHint("输入验证码");
                } else if (step == 1) {
                    if (NullCheckUtil.checkNullPoint(code)) {
                        vh.tvTitle.setText("当前绑定号码为");
                        vh.tvPhoneNum.setText(phone);
                        vh.tvPhoneNum.setVisibility(View.VISIBLE);
                        vh.tvCompleteHint.setVisibility(View.VISIBLE);
                        vh.btnGetcode.setVisibility(View.GONE);
                        vh.btnCancle.setVisibility(View.GONE);
                        vh.etCode.setVisibility(View.GONE);
                        vh.btnNext.setBackgroundResource(R.color.text_three);
                        vh.btnNext.setClickable(true);
                        vh.btnNext.setText("完成");
                        step=2;
                    }
                } else {
                    dismiss();
                }
            }
        });

        vh.etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                switch (step) {
                    case 0:
                        if (RegularUtils.isPhoneNumber(s.toString())) {
                            vh.btnNext.setBackgroundResource(R.color.text_three);
                            vh.btnNext.setClickable(true);
                            phone = s.toString();
                        } else {
                            vh.btnNext.setClickable(false);
                            vh.btnNext.setBackgroundResource(R.color.drive_line);
                        }
                        break;
                    case 1:
                        if (s.length() > 3) {
                            vh.btnNext.setBackgroundResource(R.color.text_three);
                            vh.btnNext.setClickable(true);
                            code = s.toString();
                        } else {
                            code = "";
                            vh.btnNext.setClickable(false);
                            vh.btnNext.setBackgroundResource(R.color.drive_line);
                        }
                        break;
                }
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
//            设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//            设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.sharepopupanimation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(80000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                return true;
            }
        });
    }

    static class ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.btn_cancle)
        AutoRelativeLayout btnCancle;
        @BindView(R.id.btn_getcode)
        TextView btnGetcode;
        @BindView(R.id.btn_next)
        TextView btnNext;
        @BindView(R.id.et_code)
        EditText etCode;
        @BindView(R.id.ll_orderinfo)
        AutoLinearLayout llOrderinfo;
        @BindView(R.id.tv_phone_num)
        TextView tvPhoneNum;
        @BindView(R.id.tv_complete_hint)
        TextView tvCompleteHint;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
